/*
========================================================================
 Name        : ExportContactsStartContainer.cpp
 Author      : autonavi.com
 Copyright   : autonavi.com,All Right Reserved!
 Description : 
========================================================================
*/
 
#include <barsread.h>
#include <stringloader.h>
#include <aknlists.h>
#include <eikenv.h>
#include <akniconarray.h>
#include <eikclbd.h>
#include <aknviewappui.h>
#include <eikappui.h>
#include <eiklabel.h>
#include <eikenv.h>
#include <gdi.h>
#include <eikedwin.h>
#include <ExportContacts.rsg>
#include <ExportContacts.mbg>
#include <GULICON.H>
#include <UTF.H>

#include "ExportContactsStartContainer.h"
#include "ExportContactsStartContainerView.h"
#include "ExportContactsAppUi.h"
#include "ExportContacts.hrh"
#include "ExportContactsListBox.hrh"
#include "ExportContactsSelectDirContainer.hrh"
#include "ExportContactsExportingContainer.hrh"
 
_LIT(KExportContactsFile, "\\resource\\apps\\ExportContacts.mbm" );


CExportContactsStartContainer::CExportContactsStartContainer()
{ 	
	iSpalshIcon = NULL;
	iSpalshTimer = NULL;
	iSpalshCount = 0;
}
 
CExportContactsStartContainer::~CExportContactsStartContainer()
{	 
	if(iSpalshIcon)
	{
		delete iSpalshIcon;
		iSpalshIcon = NULL;
	}
	if(iSpalshTimer)
	{
		delete iSpalshTimer;
		iSpalshTimer = NULL;
	}
}
				
 
CExportContactsStartContainer* CExportContactsStartContainer::NewL( const TRect& aRect,	const CCoeControl* aParent,MEikCommandObserver* aCommandObserver )
{
	CExportContactsStartContainer* self = CExportContactsStartContainer::NewLC( 
																				aRect, 
																				aParent, 
																				aCommandObserver );
	CleanupStack::Pop( self );
	return self;
}

 
CExportContactsStartContainer* CExportContactsStartContainer::NewLC( 
																	const TRect& aRect, 
																	const CCoeControl* aParent, 
																	MEikCommandObserver* aCommandObserver )
{
	CExportContactsStartContainer* self = new ( ELeave ) CExportContactsStartContainer();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
}
			
  
void CExportContactsStartContainer::ConstructL( 
												const TRect& aRect, 
												const CCoeControl* aParent, 
												MEikCommandObserver* aCommandObserver )
{
	if ( aParent == NULL )
	{
		CreateWindowL();
	}
	else
	{
	    SetContainerWindowL( *aParent );
	}
	iSpalshTimer = CPeriodic::NewL(CActive::EPriorityIdle);
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	InitializeControlsL();
	SetRect( aRect );
	LoadSplashIcon();
	ActivateL();
	StartSplash();
}
 
TInt CExportContactsStartContainer::CountComponentControls() const
{
	return ( int ) ELastControl;
}
				
 
CCoeControl* CExportContactsStartContainer::ComponentControl( TInt aIndex ) const
{	 
	switch ( aIndex )
	{
	}
	return NULL;
}
				
 			
void CExportContactsStartContainer::SizeChanged()
{
	CCoeControl::SizeChanged();
	LayoutControls();
}
				
 
void CExportContactsStartContainer::LayoutControls()
{
}
 				
TKeyResponse CExportContactsStartContainer::OfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
{ 
	
	if ( iFocusControl != NULL
		&& iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
	{
		return EKeyWasConsumed;
	}
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
}
				
 				
void CExportContactsStartContainer::InitializeControlsL()
{
	
}
 
void CExportContactsStartContainer::HandleResourceChange( TInt aType )
{
	CCoeControl::HandleResourceChange( aType );
	SetRect( iAvkonViewAppUi->View( TUid::Uid( EExportContactsStartContainerViewId ) )->ClientRect() );
}
				
 			
void CExportContactsStartContainer::Draw( const TRect& aRect ) const
{	 
	CWindowGc& gc = SystemGc();
//	gc.Clear( aRect );	
	if(iSpalshIcon)
	{
		TPoint spalshPosition(0,0);
		CFbsBitmap* pBitmap = iSpalshIcon->Bitmap();
		if(pBitmap)
		{
			gc.BitBlt(spalshPosition,pBitmap);
		}		
	}
	 
}

void CExportContactsStartContainer::LoadSplashIcon()
{
	TSize pFullScreenSize;
	pFullScreenSize =  Rect().Size();
	iSpalshIcon = LoadAndScaleIconL( KExportContactsFile, 
									 EMbmExportcontactsSplash, 									  
									 &pFullScreenSize,
									 EAspectRatioNotPreserved );
}

void CExportContactsStartContainer::StartSplash()
{
	if(iSpalshTimer)
	{
		iSpalshTimer->Start(0,500000,TCallBack(SplashCallback,this));
	}
}

TInt CExportContactsStartContainer::SplashCallback(TAny* aPtr)
{
	CExportContactsStartContainer* pContainer = (CExportContactsStartContainer*)aPtr;	  
	pContainer->iSpalshCount ++;
	if(pContainer->iSpalshCount > 6)
	{
		pContainer->iSpalshTimer->Cancel();	 
		CExportContactsAppUi* appUi = (CExportContactsAppUi*)(CEikonEnv::Static()->AppUi());
		appUi->ActiveView(EExportContactsListBoxViewId);
	}
	pContainer->DrawNow();
	return 1;
}

CGulIcon* CExportContactsStartContainer::LoadAndScaleIconL( 
															const TDesC& aFileName,
															TInt aBitmapId, 															 
															TSize* aSize, 
															TScaleMode aScaleMode )
{
	CFbsBitmap* bitmap; 
	bitmap = AknIconUtils::CreateIconL(aFileName, aBitmapId );
	
	TSize size;
	if ( aSize == NULL )
	{		 
		AknIconUtils::PreserveIconData( bitmap );
		AknIconUtils::GetContentDimensions( bitmap, size );
	}
	else
	{
		size = *aSize;
	}	
	AknIconUtils::SetSize( bitmap, size, aScaleMode );
	
	if ( aSize == NULL )
	{
		AknIconUtils::DestroyIconData( bitmap );
	}	
	return  CGulIcon::NewL(bitmap );   
}
